home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Essentials / MacApp Documentation / MacApp.TECH$ Archives / 1991 / Feb 91 / MacApp.Tech$ 2⁄22⁄91 / 3017-Re Write⁄Read Proble-Feb91 < prev    next >
Encoding:
Text File  |  1991-03-06  |  1.6 KB  |  59 lines  |  [TEXT/GEOL]

  1. Item    2808332                         19-Feb-91        19:58PST
  2.  
  3. From:   SATORI                          Satori SW, Hugh Rogovy,PRT
  4.  
  5. To:     MACAPP.TECH$                    MacApp Technical
  6.  
  7. ------------------------------------------------------------------------------
  8.  
  9. Sub:    Re: Write/Read Problem
  10.  
  11. Ernie,
  12.  
  13. MPW Pascal always allocates an even number of bytes for STRING types.
  14.  
  15. So in your case, the size of each element is going to be:
  16.   kMaxDataLength=10;
  17.   Size of STRING[kMaxDataLength] =
  18.     (10 character bytes)+(1 length byte)+[1 pad byte]=  12 allocated bytes
  19.  
  20. In your code, you are calculating the size of a STRING[kMaxDataLength] as 11
  21. bytes, causing you to write a partial data structure to disk.
  22.  
  23. You got lucky with 7 elements, because your 7th string must have been <5
  24. characters long.
  25.  
  26. For odd length strings (ie. STRING[5]), you'll never have this problem because
  27. string length + length byte adds up to an even value.
  28.  
  29. The better way to write your code is:
  30.  
  31.  
  32. CONST
  33.     kMaxDataLength=10;
  34.     kMaxPoints=400;
  35. TYPE
  36.     dataElementType   =   String[kMaxDataLength];
  37. VAR
  38.     aDataArray: ARRAY[1..kMaxPoints] of dataElementType;
  39.  
  40. WRITING:
  41.    Count:=SIZEOF(dataElementType)*fNumberTimeUnits;
  42.    FailOSErr(FSWrite(aRefNum,Count,@aDataArray));
  43.  
  44. READING:
  45.    Count:=SIZEOF(dataElementType)*fNumberTimeUnits;
  46.    FailOSErr(FSRead(aRefNum,Count,@aDataArray));
  47.  
  48. You need to define your string as a TYPE because the Pascal compiler can't
  49. handle SIZEOF(STRING[kMaxDataLength]).  It's always a good idea to use SIZEOF,
  50. that way you don't need to make assumptions about how data is allocated by the
  51. compiler.
  52.  
  53.  
  54. Hope this helps,
  55.  
  56. Chris Le Croy
  57. Satori Software
  58.  
  59.